package view;

import java.awt.Color;
import java.awt.Container;
import java.awt.Font;
import java.awt.event.InputMethodEvent;
import java.awt.event.InputMethodListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;

import controller.InschrijvingController;
import controller.OpstartController;

public class CursistFrame extends JFrame {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private final OpstartController opstartcontroller;
	private final InschrijvingController inschrijvingscontroller;
	private final JButton btnAdd;
	private final JComboBox<String> cmbTypeCursist;
	private final JLabel lblKeuze;
	private final JLabel lblCursistNummer;
	private final JLabel lblNaam;
	private final JLabel lblVariabel1;
	private final JLabel lblVariabel2;
	private final JTextField tfCursistNummer;
	private final JTextField tfNaam;
	private final JTextField tfVariabel1;
	private final JTextField tfVariabel2;

	/**
	 * Create the frame.
	 */
	public CursistFrame(OpstartController opstartController, InschrijvingController inschrijvingController) {
		super("Inschrijving schooljaar 2011-2012");
		this.opstartcontroller = opstartController;
		this.inschrijvingscontroller = inschrijvingController;
		Container container = getContentPane();
		container.setBackground(new Color(255, 250, 205));
		container.setLayout(null);
		this.setSize(500, 400);

		// Keuze van de cursist
		lblKeuze = new JLabel("Kies het type cursist");
		lblKeuze.setFont(new Font("Tahoma", Font.PLAIN, 15));
		lblKeuze.setBounds(33, 11, 143, 20);

		String[] typeCursisten = { "", "Vrije Cursist", "Herscholingscursist" };
		cmbTypeCursist = new JComboBox<String>(typeCursisten);
		cmbTypeCursist.setBounds(33, 49, 178, 20);

		// In te vullen parameters
		lblCursistNummer = new JLabel("Cursist Nummer");
		lblCursistNummer.setVisible(false);
		lblCursistNummer.setBounds(33, 108, 143, 14);

		lblNaam = new JLabel("Naam");
		lblNaam.setVisible(false);
		lblNaam.setBounds(33, 139, 143, 14);

		lblVariabel1 = new JLabel();
		lblVariabel1.setVisible(false);
		lblVariabel1.setBounds(33, 169, 143, 14);

		lblVariabel2 = new JLabel();
		lblVariabel1.setVisible(false);
		lblVariabel2.setBounds(33, 200, 143, 14);

		tfCursistNummer = new JTextField();
		tfCursistNummer.setVisible(false);
		tfCursistNummer.setBounds(232, 105, 203, 20);
		tfCursistNummer.setColumns(10);

		tfNaam = new JTextField();
		tfNaam.setVisible(false);
		tfNaam.setBounds(232, 136, 203, 20);
		tfNaam.setColumns(10);

		tfVariabel1 = new JTextField();
		tfVariabel1.setVisible(false);
		tfVariabel1.setBounds(232, 166, 203, 20);
		tfVariabel1.setColumns(10);

		tfVariabel2 = new JTextField();
		tfVariabel2.setVisible(false);
		tfVariabel2.setBounds(232, 197, 203, 20);
		tfVariabel2.setColumns(10);

		btnAdd = new JButton("");
		btnAdd.setVisible(false);
		btnAdd.setBounds(100, 275, 300, 30);

		// Componenten in container laden
		container.add(lblKeuze);
		container.add(cmbTypeCursist);
		container.add(lblCursistNummer);
		container.add(lblNaam);
		container.add(lblVariabel1);
		container.add(lblVariabel2);
		container.add(tfCursistNummer);
		container.add(tfNaam);
		container.add(tfVariabel1);
		container.add(tfVariabel2);
		container.add(btnAdd);

		// Eventhandlers
		cmbTypeCursist.addItemListener(new ItemListener() {
			@Override
			public void itemStateChanged(ItemEvent event) {
				String selectedItm = cmbTypeCursist.getSelectedItem().toString();
				if (selectedItm.equalsIgnoreCase("vrije cursist")) {
					lblCursistNummer.setVisible(true);
					lblNaam.setVisible(true);
					lblVariabel1.setText("Nationaliteit:");
					lblVariabel1.setVisible(true);
					lblVariabel2.setVisible(false);
					tfCursistNummer.setVisible(true);
					tfNaam.setVisible(true);
					tfVariabel1.setVisible(true);
					tfVariabel2.setVisible(false);
					btnAdd.setVisible(true);
					btnAdd.setText(getRegistratieCursist());
				} else if (selectedItm.equalsIgnoreCase("Herscholingscursist")) {
					lblCursistNummer.setVisible(true);
					lblNaam.setVisible(true);
					lblVariabel1.setText("Reden Herscholing:");
					lblVariabel1.setVisible(true);
					lblVariabel2.setText("Hoogst Behaalde Diploma:");
					lblVariabel2.setVisible(true);
					tfCursistNummer.setVisible(true);
					tfNaam.setVisible(true);
					tfVariabel1.setVisible(true);
					tfVariabel2.setVisible(true);
					btnAdd.setVisible(true);
					btnAdd.setText(getRegistratieCursist());
				} else {
					lblVariabel1.setText("");
					lblVariabel2.setText("");
					lblCursistNummer.setVisible(false);
					lblNaam.setVisible(false);
					lblVariabel1.setVisible(false);
					lblVariabel2.setVisible(false);
					tfCursistNummer.setVisible(false);
					tfNaam.setVisible(false);
					tfVariabel1.setVisible(false);
					tfVariabel2.setVisible(false);
					btnAdd.setVisible(false);
					btnAdd.setText("");
				}
			}

		});
		tfCursistNummer.addInputMethodListener(new InputMethodListener() {
			@Override
			public void caretPositionChanged(InputMethodEvent event) {
				// TODO Auto-generated method stub

			}

			@Override
			public void inputMethodTextChanged(InputMethodEvent event) {
				btnAdd.setText(getRegistratieCursist());

			}
		});
		tfNaam.addInputMethodListener(new InputMethodListener() {
			@Override
			public void caretPositionChanged(InputMethodEvent event) {
				// TODO Auto-generated method stub

			}

			@Override
			public void inputMethodTextChanged(InputMethodEvent event) {
				btnAdd.setText(getRegistratieCursist());

			}
		});

	}

	// Methods
	private String getRegistratieCursist() {
		String tempString = "Add:";
		if (!tfCursistNummer.getText().isEmpty() && !tfNaam.getText().isEmpty()) {
			tempString = "Cursistnummer: " + tfCursistNummer.getText() + "    Naam: " + tfNaam.getText();
		}
		return tempString;
	}
}
